/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-09-14
 * Time: 9:24
 */

//1.不能存放各种类型的元素
class MyArrayList1 {
    private int[] elem;
    private int usedSize;

    public MyArrayList1() {
        this.elem = new int[10];
    }

    public void add(int val){
        this.elem[usedSize] = val;
        usedSize++;
    }
    public int get(int pos){
        return this.elem[pos];
    }
}
//3.存放元素不能指定元素
//4.每次取出数据都需要进行强制类型转换
class MyArrayList2 {
    private Object[] elem;
    private int usedSize;

    public MyArrayList2() {
        this.elem = new Object[10];
    }

    public void add(Object val){
        this.elem[usedSize] = val;
        usedSize++;
    }
    public Object get(int pos){
        return this.elem[pos];
    }
}
//指定顺序表数据类型
//取出数据不需要进行强制类型转换
//<E>代表当前类是一个泛型类，E是一个占位符
//泛型：把类型参数化了
    //意义：
    //1.自动对类型进行检查
    //2.自动对类型进行强转
    //3.泛型不参与数据类型的组成
//泛型是怎么编译的？
    //泛型是编译时期的一种机制，擦除机制 - 变成Object
class MyArrayList3 <E>{
    private E[] elem;
    private int usedSize;

    public MyArrayList3() {
        this.elem = (E[]) new Object[10];
        //this.elem = new E[10];
    }

    public void add(E val){
        this.elem[usedSize] = val;
        usedSize++;
    }
    public E get(int pos){
        return this.elem[pos];
    }
}
public class TestDemo2 {
    public static void main4(String[] args) {
        MyArrayList3<String> myArrayList3 = new MyArrayList3<>();
        System.out.println(myArrayList3);
        MyArrayList3<Integer> myArrayList31 = new MyArrayList3<>();
        System.out.println(myArrayList31);
        MyArrayList3<Boolean> myArrayList32 = new MyArrayList3<>();
        System.out.println(myArrayList32);

    }
    public static void main3(String[] args) {
        MyArrayList3<String> myArrayList3 = new MyArrayList3<>();
        myArrayList3.add("ABC");
        myArrayList3.add("bit");
        //myArrayList3.add(10);
        String ret = myArrayList3.get(1);
        System.out.println(ret);

    }
    public static void main2(String[] args) {
        MyArrayList3<String> myArrayList3 = new MyArrayList3<>();
        MyArrayList3<Integer> myArrayList31 = new MyArrayList3<>();
        MyArrayList3<Boolean> myArrayList32 = new MyArrayList3<>();
    }
    public static void main(String[] args) {
        MyArrayList2 myArrayList2 = new MyArrayList2();
        myArrayList2.add(1);
        myArrayList2.add("hello");
        String ret = (String) myArrayList2.get(1);
        System.out.println(ret);
    }
}
